<template>
  <view class="qualification">
    <nav-bar title="资质认证"></nav-bar>
    <scroll-view scroll-y :style="{ height: scrollHeight }">
      <view class="content-box">
        <view class="form-box">
          <you-form
            ref="form"
            :showFormTitle="false"
            :formConfig="qualificationConfig"
            v-model="qualificationData"
          ></you-form>
        </view>
        <view class="tip">照片仅用作认证，平台会保护您的隐私，不会泄露给第三方。</view>
      </view>
    </scroll-view>
    <you-bottom-btn :btnConfig="bottomBtnConfig" @clickSubmit="clickSubmit"></you-bottom-btn>
  </view>
</template>

<script>
  import { QUALIFICATION_FORM } from '../formConfig/qualificationConfig'
  import api_physician from '@/api/physician.js'
  import api_login from '@/api/login.js'
  export default {
    name: 'MyQualification',

    data() {
      return {
        scrollHeight: '',
        qualificationConfig: [],
        qualificationData: {},
        bottomBtnConfig: [
          { text: '提交', class: 'confirm', funcName: 'clickSubmit' }
        ]
      }
    },
    onLoad() {
      this.scrollHeight = this.$store.getters.heightData.noTabMainHeight - 85+ 'px'
      this.userInfo = uni.getStorageSync('allUserInfo')
      this.qualificationConfig = uni.$u.deepClone(QUALIFICATION_FORM)
      this.qualificationData = this.$u.deepClone(this.userInfo)
      this.getTagList()
    },
    methods: {
      getTagList() {
        // const params ={
        //   type: 2 // 职称的标签
        // }
        api_physician.fetchTagList().then(res => {
          if (res.code !== 200) return
          this.qualificationConfig.forEach(item => {
            if (item.key === 'professionalTitle') {
              item.options = res.rows.filter(rItem => rItem.type === '2').map(rrItem => {
                return {
                  text: rrItem.tagName,
                  value: rrItem.tagName
                }
              })
            }
            if (item.key === 'department') {
              item.options = res.rows.filter(rItem => rItem.type === '3').map(rrItem => {
                return {
                  text: rrItem.tagName,
                  value: rrItem.tagName
                }
              })
            }
          })
        })
      },
      async clickSubmit() {
        const formCheck = await this.$refs.form.checkFormData()
        if (!formCheck) return uni.$u.toast('请将表单数据填写完整')
        const params = {
          ...this.qualificationData
        }
        api_physician.updatePhysicianInfo(params).then(res => {
          if (res.code !== 200) return
          uni.showToast({
            title: '更新成功',
            icon: 'none'
          })
          api_login.fetchUserInfo().then(res => {
            if (res.code !== 200) return
            const userInfo = {
              baUserId: res.baUser.id,
              ...res.baUser,
              ...res.doctorInfo
            }
            uni.setStorageSync('allUserInfo', userInfo)
          })
          uni.reLaunch({
            url: '/pages/physicianMine'
          })
        })
      }
    }
  }
</script>
<style lang="scss">
  .qualification {
    height: 100vh;
    background-color: #F0F3F5;
    .content-box {
      padding-top: 30rpx;
      .form-box {
        margin: 0 30rpx;
        border-radius: 16rpx;
        background-color: #FFF;
        overflow: hidden;
      }
      .tip {
        padding: 30rpx;
        font-size: 24rpx;
        color: #FF513A;
      }
    }
  }
</style>
